package com.woniuxy.hotel.room.service.imp;

import java.util.List;

import com.woniuxy.hotel.room.pojo.Evaluate;
import com.woniuxy.hotel.room.util.PageInfo;
import org.springframework.stereotype.Service;

import com.woniuxy.hotel.room.mapper.HotelMapper;
import com.woniuxy.hotel.room.pojo.Hotel;
import com.woniuxy.hotel.room.pojo.HotelExample.Criteria;
import com.woniuxy.hotel.room.pojo.HotelWithBLOBs;
import com.woniuxy.hotel.room.pojo.HotelExample;
import com.woniuxy.hotel.room.service.HotelService;

import javax.annotation.Resource;

@Service
public class HotelServiceImp implements HotelService {
	
	@Resource
	private HotelMapper hotelMapper;
	
	/*
	 * 
	 * 
	 * 查找所有酒店
	 * */
	@Override
	public List<HotelWithBLOBs> findHotel() {
		HotelExample example=new HotelExample();
		return hotelMapper.selectByExampleWithBLOBs(example);
	}
	
	/**
	 * 查询分页所需酒店信息（包含平均平均星级）
	 * @return
	 */
	public List<HotelWithBLOBs> limitHotelInfo(Integer page,Integer limit){
		PageInfo pageInfo =new PageInfo(page,limit);
		List<HotelWithBLOBs> list=hotelMapper.selectWithEvaluatesByLimitWithBLOBs(pageInfo);
		for (HotelWithBLOBs hotelWithBLOBs : list) {
			int sum=0;
			List<Evaluate> evaluates=hotelWithBLOBs.getEvaluates();
			if(evaluates.size()>0) {
				for (Evaluate evaluate : evaluates) {
					sum += evaluate.getStar();
				}
				double avg = sum / (evaluates.size());
				hotelWithBLOBs.setStar(avg);
			}else {
				hotelWithBLOBs.setStar(0.0);
			}
		}
		return list;
	}
	
	/**
	 * 统计数据库数据条数（去除软删除后
	 * @return
	 */
	public Integer countNum(){
		return hotelMapper.countByExample(null);
	}
	/*
	 * 查找酒店
	 * 动态Sql
	 * 支持所有字段
	 * 
	 * */
	@Override
	public List<Hotel> findHotelSql(Hotel hotel) {
		HotelExample example=new HotelExample();
		Criteria criteria=example.createCriteria();
		criteria.andHotelidEqualTo(hotel.getHotelid());
		criteria.andHotelnameEqualTo(hotel.getHotelname());
		criteria.andHoteladdrEqualTo(hotel.getHoteladdr());
		criteria.andPerformanceEqualTo(hotel.getPerformance());
				return hotelMapper.selectByExample(example);
	}
	
	@Override
	public HotelWithBLOBs findAllHotelWithBLOBs(Hotel hotel) {
		
		return hotelMapper.selectWithImgByPrimaryKey(hotel.getHotelid());
		
	}

}
